// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once
#include <pollux/expression/eval_ctx.h>

namespace kumo::pollux::exec {
    /// A helper class to decode VectorFunction arguments.
    /// Example:
    ///    DecodedArgs decodedArgs(rows, args, context);
    ///    auto base = decodedArgs.at(0);
    ///    auto exp = decodedArgs.at(1);
    ///
    ///    rows.applyToSelected([&](int row) {
    ///      rawResults[row] =
    ///        std::pow(base->value_at<double>(row), exp->value_at<double>(row));
    ///    });
    ///
    class DecodedArgs {
    public:
        DecodedArgs(
            const SelectivityVector &rows,
            const std::vector<VectorPtr> &args,
            exec::EvalCtx &context) {
            for (auto &arg: args) {
                holders_.emplace_back(context, *arg, rows);
            }
        }

        DecodedVector *at(int i) const {
            return const_cast<exec::LocalDecodedVector *>(&holders_[i])->get();
        }

        size_t size() const {
            return holders_.size();
        }

    private:
        std::vector<exec::LocalDecodedVector> holders_;
    };
} // namespace kumo::pollux::exec
